#include "gm_sc_app.h"

gm_sc_app::gm_sc_app()
{
	memset(_name, 0, sizeof(_name));
	_ulid = 0;
	_bverify = 0;
	_is_verify_pin = 0;
	_is_cert_valid = 1;

	init_handle(APP_HANDLE);
}
gm_sc_app::gm_sc_app(AppInfoRsp info, char *pname)
{
	memset(_name, 0, sizeof(_name));
	strcpy(_name, pname);
	_ulid = info.wAppID;
	_bverify = 0;
	_is_verify_pin = 0;

	memcpy(&_app_info, &info, sizeof(AppInfoRsp));

	init_handle(APP_HANDLE);
}
gm_sc_app::~gm_sc_app(void)
{
}

ULONG gm_sc_app::IsVerify()
{
	if(!_is_verify_pin)
		return TRUE;
	return _bverify;
}

void gm_sc_app::IsVerify(ULONG val) 
{
	_bverify = val; 
}


ULONG gm_sc_app::not_verify_pin()
{
	_is_verify_pin = 0;
	return 0;
}


gm_sc_key * gm_sc_app::find_key( HANDLE hKey, ULONG * containerId )
{
	gm_sc_key * pKey = NULL;
	gm_sc_cont_mgr *cont_mgr = get_cont_mgr();
	gm_sc_cont *sc_cont = cont_mgr->get_first_cont();
	while(sc_cont)
	{
		pKey = sc_cont->find_key(hKey);
		if(pKey)
		{
			* containerId = sc_cont->id();
			break;
		}

		sc_cont = cont_mgr->get_next_cont();
	}

	return pKey;
}

gm_sc_key * gm_sc_app::find_key( HANDLE hKey, gm_sc_cont *& pContainer )
{
	gm_sc_key * pKey = NULL;
	gm_sc_cont_mgr *cont_mgr = get_cont_mgr();
	gm_sc_cont *sc_cont = cont_mgr->get_first_cont();
	while(sc_cont)
	{
		pKey = sc_cont->find_key(hKey);
		if(pKey)
		{
			pContainer = sc_cont;
			break;
		}

		sc_cont = cont_mgr->get_next_cont();
	}

	return pKey;
}

gm_sc_mac * gm_sc_app::find_mac( HANDLE hMac, gm_sc_cont *& pContainer )
{
	gm_sc_mac * pMac = NULL;
	gm_sc_cont_mgr *cont_mgr = get_cont_mgr();
	gm_sc_cont *sc_cont = cont_mgr->get_first_cont();
	while(sc_cont)
	{
		pMac = sc_cont->find_mac(hMac);
		if(pMac)
		{
			pContainer = sc_cont;
			break;
		}

		sc_cont = cont_mgr->get_next_cont();
	}

	return pMac;
}

gm_sc_cont * gm_sc_app::find_container( HCONTAINER hContainer )
{
	gm_sc_cont_mgr *cont_mgr = get_cont_mgr();
	gm_sc_cont *sc_cont = cont_mgr->get_first_cont();
	while(sc_cont)
	{
		if(sc_cont->get_handle() == hContainer)
		{
			return sc_cont;
		}

		sc_cont = cont_mgr->get_next_cont();
	}

	return NULL;
}

gm_sc_cont * gm_sc_app::find_container_by_name( char * name )
{
	gm_sc_cont_mgr *cont_mgr = get_cont_mgr();
	gm_sc_cont *sc_cont = cont_mgr->get_first_cont();
	while(sc_cont)
	{
		if(strcmp(sc_cont->Name(), name) == 0)
		{
			return sc_cont;
		}

		sc_cont = cont_mgr->get_next_cont();
	}

	return NULL;
}

gm_sc_cont * gm_sc_app::create_container( int cont_id, LPSTR containerName )
{
	gm_sc_cont *pcont = new gm_sc_cont(cont_id, containerName, _ulid);
	_sc_con.add_cont(pcont);

	return pcont;
}

ULONG gm_sc_app::remove_container(gm_sc_cont *pcont)
{
	return _sc_con.remove_cont(pcont);
}

ULONG gm_sc_app::remove_container( LPSTR szContainerName )
{	
	return _sc_con.remove_cont(szContainerName);
}

gm_sc_cont * gm_sc_app::get_first_container()
{
	return _sc_con.get_first_cont();
}

ULONG gm_sc_app::not_check_cert_valid()
{
	_is_cert_valid = 0;
	return 0;
}

ULONG gm_sc_app::is_cert_valid()
{
	return _is_cert_valid;
}
